import pandas as pd
from PySide2.QtCore import QDir

from forms.uipy.UI_ExcelFindUser import Ui_ExcelFindUser
from PySide2.QtWidgets import QWidget, QLabel, QTableView, QTableWidget, QTableWidgetItem, QMenu, QAction, QInputDialog, \
    QLineEdit
from PySide2.QtGui import QStandardItemModel, QStandardItem, Qt

from tools.File_Select_Tools import File_Select_Tools
from server.ExcelFindUserServer import ExcelFindUserServer
from PySide2.QtWidgets import QMessageBox


class ExcelFindUserImpl(QWidget, Ui_ExcelFindUser):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        # 绑定文件选择按钮
        self.file_selected_btn.clicked.connect(self.file_selected_btn_handler)

        # 绑定运行按钮
        self.find_user_btn.clicked.connect(self.__find_user__)
        # 清空所有tab
        self.show_excel.clear()
        # 构建表格数据

        self.show_excel.setContextMenuPolicy(Qt.ActionsContextMenu)
        send_option = QAction(self.show_excel)
        send_option.setText("输出文件")
        send_option.triggered.connect(self.out_file)  # 点击菜单中的“发送控制代码”执行的函数
        self.show_excel.addAction(send_option)

        self.excelFindUserServer = None
    def file_selected_btn_handler(self):
        file_path = File_Select_Tools.select_one_file(title="Excel文件选择", fileter_type="xlsx file(*.xlsx)")[0]
        self.excel_path.setText(file_path)
        # 设置输出文件默认名称
        file_name = file_path[file_path.rfind("/") + 1:file_path.rfind(".")]
        self.file_name.setText(file_name)

    def __find_user__(self):
        """
        点击运行按钮执法方法
        :return:
        """
        # 获取excel_path
        if len(self.excel_path.text()) == 0:
            # 显示错误消息
            QMessageBox.information(None, "错误提示", "Excel路径不能为空")

        #     获取文本数据 并格式化为列表
        text = self.user_ids.text()
        ids = text.split("\n")
        # ids.remove("")
        self.excelFindUserServer = ExcelFindUserServer(self.excel_path.text())
        # 获取查找的数据并显示
        self.__show_table_data__(self.excelFindUserServer.find_user(ids=ids))
        # 处理数据，并输出excel
        # ExcelFindUserServer(self.excel_path.text()).find_user_one_to_excel(file_name=self.file_name.text(), ids=ids)
        # QMessageBox.information(None, "成功提示", "执行完成！")



    def out_file(self):
        # 输出到文件
        # 获取用户选择的目录路径
        file_dir = File_Select_Tools.select_dir()
        self.excelFindUserServer.to_excel(file_dir,self.file_name.text())
        pass


    def __show_table_data__(self,data):
        # 清空所有tab
        self.show_excel.clear()
        for key in data.keys():
            # 空数据不显示
            if len(data[key]) == 0:
                continue
            table = QTableWidget()
            # 设置行数和列宽
            table.setRowCount(len(data[key]))
            table.setColumnCount(len(data[key].keys()))
            table.setHorizontalHeaderLabels(list(data[key].keys()))
            for i, row in data[key].iterrows():
                for j in range(len(row)):
                    # 设置数据
                    table.setItem(i, j, QTableWidgetItem(str(row[j])))
            self.show_excel.addTab(table, key)
